﻿Imports System.Data
Imports System.Data.OleDb

Namespace DoFactory.GangOfFour.Template.RealWorld

    ''' <summary>
    ''' MainApp startup class for Real-World 
    ''' Template Design Pattern.
    ''' </summary>
    Public Class MainApp

        ''' <summary>
        ''' Entry point into console application.
        ''' </summary>
        Public Shared Sub Main()

            Dim daoCategories As DataAccessObject = New Categories()
            daoCategories.Run()

            Dim daoProducts As DataAccessObject = New Products()
            daoProducts.Run()

            ' Wait for user
            'Console.ReadKey()

        End Sub

    End Class



    ''' <summary>
    ''' The 'AbstractClass' abstract class
    ''' </summary>
    MustInherit Class DataAccessObject

        Protected connectionString As String

        Protected dataSet As DataSet



        Public Overridable Sub Connect()

            ' Make sure mdb is available to app
            connectionString = "provider=Microsoft.JET.OLEDB.4.0; " & "data source=..\..\..\db1.mdb"

        End Sub


        Public MustOverride Sub [Select]()

        Public MustOverride Sub Process()

        Public Overridable Sub Disconnect()
            connectionString = ""
        End Sub

        ' The 'Template Method' 
        Public Sub Run()
            Connect()
            [Select]()
            Process()
            Disconnect()
        End Sub

    End Class

    ''' <summary>
    ''' A 'ConcreteClass' class
    ''' </summary>
    Class Categories
        Inherits DataAccessObject


        Public Overrides Sub [Select]()
            Dim sql As String = "select CategoryName from Categories"

            Dim dataAdapter As New OleDbDataAdapter(sql, connectionString)

            dataSet = New DataSet()

            dataAdapter.Fill(dataSet, "Categories")

        End Sub

        Public Overrides Sub Process()

            Console.WriteLine("Categories ---- ")

            Dim dataTable As DataTable = dataSet.Tables("Categories")

            For Each row As DataRow In dataTable.Rows
                Console.WriteLine(row("CategoryName"))
            Next

            Console.WriteLine()

        End Sub

    End Class



    ''' <summary>
    ''' A 'ConcreteClass' class
    ''' </summary>
    Class Products
        Inherits DataAccessObject

        Public Overrides Sub [Select]()
            Dim sql As String = "select ProductName from Products"

            Dim dataAdapter As New OleDbDataAdapter(sql, connectionString)

            dataSet = New DataSet()

            dataAdapter.Fill(dataSet, "Products")

        End Sub

        Public Overrides Sub Process()
            Console.WriteLine("Products ---- ")

            Dim dataTable As DataTable = dataSet.Tables("Products")

            For Each row As DataRow In dataTable.Rows
                Console.WriteLine(row("ProductName"))
            Next

            Console.WriteLine()

        End Sub

    End Class

End Namespace
